package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicQkDAO;
import com.cssy.yd.logic.dao.LogicQkItemDAO;
import com.cssy.yd.logic.entity.LogicQkDO;
import com.cssy.yd.logic.entity.LogicQkItemDO;
import com.cssy.yd.logic.service.ILogicQkService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtil;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Service("logicQkService")
public class LogicQkServiceImpl implements ILogicQkService {
	
	@Resource(name = "logicQkDAO")
	LogicQkDAO logicQkDAO;
	
	@Resource(name = "logicQkItemDAO")
	LogicQkItemDAO logicQkItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		// TODO Auto-generated method stub
		SearchResult rs=new SearchResult(scs);
		List<LogicQkDO>  list=logicQkDAO.find(scs);
		rs.setRecordCount(logicQkDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		logicQkDAO.deleteLogicQkDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicQkDO t, User user) throws SyBaseException {
		t.setVersion(t.getVersion()+1);
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		logicQkDAO.updateLogicQkDO(t);
		return null;
	}

	@Override
	public LogicQkDO save(LogicQkDO t, User user) throws SyBaseException {
		t.setVersion(Double.parseDouble("1"));
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		t.setId(IDCodeTypeUtil.getId());
		logicQkDAO.insertLogicQkDO(t);
		return t;
	}

	@Override
	public LogicQkDO findListById(String id) throws SyBaseException {
		return logicQkDAO.findLogicQkDOByPrimaryKey(id);
	}

	@Override
	public void qkSave(HttpServletRequest request, LogicQkDO curAP)
			throws SyBaseException {
		try {
			String itemdata =request.getParameter("itemdata");
			String itemdelete =request.getParameter("itemdelete");
			String qkrq=request.getParameter("qkrq");
			curAP.setQkrq(DateUtils.stringToDate(qkrq));
//			if(!"".equals(type)&&type!=null){
//				curAP.setBillstatus(Short.parseShort(type));
//			}
			List<LogicQkItemDO> lp = JSONArray.parseArray(itemdata,
					LogicQkItemDO.class);//LogicPoApplyItemDO
			if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改
				int result =logicQkDAO.updateLogicQkDO(curAP);
				if(1==result){
					if (itemdelete != null && !"".equals(itemdelete)) {
						List<LogicQkItemDO> lpiem = JSONArray.parseArray(itemdelete, LogicQkItemDO.class);
						for (int i = 0; i < lpiem.size(); i++) {
							if (lpiem.get(i).getId() != null&& !"".equals(lpiem.get(i).getId())) {
								logicQkItemDAO.deleteLogicQkItemDOByPrimaryKey(lpiem.get(i).getId());
							}
						}
					}
					for (int i = 0; i < lp.size(); i++) {
						lp.get(i).setQkid(curAP.getId());
						if (lp.get(i).getId() != null&& !"".equals(lp.get(i).getId())) {
							logicQkItemDAO.updateLogicQkItemDO(lp.get(i));
						} else {
							lp.get(i).setId(IDCodeTypeUtil.getId());
							logicQkItemDAO.insertLogicQkItemDO(lp.get(i));
						}
					}
				}else{
					throw new SyBaseException("您修改的行数据发生改变，请重新点开查看");
				}
			} else {
				String id = IDCodeTypeUtil.getId();
				curAP.setQkcode(syCodeService.getCode("QK","QK",SyCode.DATE_YYMM,5));
				curAP.setDcruser(SessionUtil.getUser(request).getName());
				curAP.setDctime(new Date());
				curAP.setId(id);
				logicQkDAO.insertLogicQkDO(curAP);
				if (lp.size() > 0){
					for (int i = 0;i < lp.size();i++){
						lp.get(i).setId(IDCodeTypeUtil.getId());
						lp.get(i).setQkid(id);
						logicQkItemDAO.insertLogicQkItemDO(lp.get(i));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
